package com.kevin.design.creationalPattern.prototype;

public class ShallowClone {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person personPrototype = new Person("zhangsan","man",25);
        personPrototype.setGirlFriend(new GirlFriend("xiaohong","woman",25));
        Person personClone = (Person) personPrototype.clone();
        System.out.println(personClone.toString());
        System.out.println(personPrototype == personClone);
        System.out.println(personPrototype.getGirlFriend() == personClone.getGirlFriend());
    }
}
class GirlFriend{
    private String name;
    private String gender;
    private Integer age;
    public GirlFriend(String name, String gender, Integer age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    @Override
    public String toString() {
        return String.format("Person[name='%s',gender='%s',age=%d]",name,gender,age);
    }
}
class Person implements Cloneable{
    private String name;
    private String gender;
    private Integer age;
    private GirlFriend girlFriend;

    public GirlFriend getGirlFriend() {
        return girlFriend;
    }

    public void setGirlFriend(GirlFriend girlFriend) {
        this.girlFriend = girlFriend;
    }

    public Person(String name, String gender, Integer age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return String.format("Person[name='%s',gender='%s',age=%d,girlFriend='%s']",name,gender,age,girlFriend);
    }
}
